import { defineConfig, Plugin, loadEnv, ConfigEnv, UserConfigExport } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
import UnoCSS from 'unocss/vite';
// element-plus
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
// 打包
import viteCompression from 'vite-plugin-compression';
// mock
import { viteMockServe } from 'vite-plugin-mock';

export default ({ mode }: ConfigEnv): UserConfigExport => {
  const env = loadEnv(mode, process.cwd());
  const useMock = env.VITE_USE_MOCK == 'true' || false;
  const plugins: (Plugin | Plugin[])[] = [
    vue(),

    UnoCSS({
      /* options */
    }),
    AutoImport({
      imports: ['vue', 'vue-router'], // 指定类型声明文件路径, 为true是根目录
      dts: 'types/auto-imports.d.ts',
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      // 指定类型声明文件路径, 为true是根目录
      dts: 'types/components.d.ts', // 开启组件名称包含目录名
      directoryAsNamespace: true,
      resolvers: [ElementPlusResolver()],
    }),
    viteMockServe({
      // default
      mockPath: './src/mock',
      enable: useMock,
    }),
    {
      ...viteCompression(),
      // 默认serve 和build都会调用 可以进行设置更改
      apply: 'serve',
    },
  ];
  // mode === 'development' && process.env.VITE_USE_MOCK === 'true' && plugins.push(vitePluginMock());
  return defineConfig({
    // 插件
    plugins: plugins,
    base: './',
    // 路径别名
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'), // @代替src
        '#': path.resolve(__dirname, './types'), // #代替types
      },
    },
    // 样式
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/styles/variate.scss";',
        },
      },
    },
    // 代理
    server: {
      host: '0.0.0.0',
      port: 8080,
      // open: true, //  测试环境打开
      https: false,
      proxy: {
        [env.VITE_BASE_URL]: {
          target: 'http://vapi.youlai.tech',
          changeOrigin: true,
          ws: true,
          // rewrite: (path: string) => path.replace(/^\/api/, ''),
        },
      },
    },
    // 分包
    build: {
      chunkSizeWarningLimit: 2000, // 消除打包大小超过500kb警告
      minify: 'terser', // Vite 2.6.x 以上需要配置 minify: "terser", terserOptions 才能生效
      rollupOptions: {
        output: {
          // js和css 文件夹分离
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
          manualChunks: {
            vue: ['vue', 'pinia', 'vue-router'],
            elementIcons: ['@element-plus/icons-vue'],
          },
        },
      },

      // terserOptions: {
      //   compress: {
      //     keep_infinity: true, // 防止 Infinity 被压缩成 1/0，这可能会导致 Chrome 上的性能问题
      //     drop_console: true, // 生产环境去除 console
      //     drop_debugger: true, // 生产环境去除 debugger
      //   },
      // },
    },
  });
};
